// pages/testindex/testindex.js
import { request } from '../../request/index'
import { categoryListJSON } from '../../categoryListJSON'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userId: wx.getStorageSync('user-id'),
  },
  getLoginStatus() {
    wx.checkSession({
      success: (result) => {
        console.log(result)
      },
      fail: () => {},
      complete: () => {},
    })
  },
  getUserInfo(res) {
    wx.showLoading()
    console.log(res, 'getuser')
    const params = { ...res.detail }
    params.code = wx.getStorageSync('code')

    wx.request({
      url: 'http://127.0.0.1:3001/weixin/login',
      data: params,
      header: { 'content-type': 'application/json' },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: (result) => {
        console.log(result)
        wx.setStorageSync('token', result.data.token)
        wx.hideLoading()
      },
      fail: () => {},
      complete: () => {},
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.login({
      timeout: 10000,
      success: (result) => {
        if (result.code) {
          console.log(result)
          wx.setStorageSync('code', result.code)
        }
      },
      fail: () => {},
      complete: () => {},
    })
  },
  getServeStatus() {
    wx.request({
      url: 'http://127.0.0.1:3001/weixin/checklogin',
      data: {
        token: wx.getStorageSync('token'),
      },
      header: { 'content-type': 'application/json' },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: (result) => {
        console.log(result)
      },
      fail: () => {},
      complete: () => {},
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function (e) {
    console.log('object')
    console.log(e)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
})
